package cook.utils;

import static cook.services.SolrService.INGREDIENT_PREFIX;

import java.util.Arrays;
import java.util.Map;

import com.google.common.base.Function;
import com.google.common.collect.Maps;

//TODO: this should really go to the DB
//TODO: use real density, not some made up numbers;
public class Converter {
    private static final Double DEF_QNT = 100.;

    public static final String[] UNITS = new String[] { "kilos", "kilo", "kg", "gr", "g", "gram", "grams", "liter",
            "liters", "l", "ml", "slice", "slices", "lb", "lbs", "pound", "pounds", "pieces", "piece", "cup", "cups",
            "spoon", "spoons" };

    // blissfully ignoring density :)
    private final static Map<String, Double> unitsToGrams;
    static {
        unitsToGrams = Maps.toMap(Arrays.asList(UNITS), new Function<String, Double>() {

            @Override
            public Double apply(final String input) {
                switch (input) {
                case "kilos":
                case "kilo":
                case "kg":
                    return 1000.;
                case "gr":
                case "g":
                case "gram":
                case "grams":
                    return 1.;
                case "liter":
                case "liters":
                case "l":
                    return 1000.;
                case "ml":
                    return 1.;
                case "lbs":
                case "lb":
                case "pound":
                case "pounds":
                    return 453.6;
                case "cup":
                case "cups":
                    return 236.6;
                case "spoon":
                case "spoons":
                    return 15.0;
                case "slices":
                case "slice":
                    return 25.;
                default:
                    return DEF_QNT;
                }
            }

        });
    }

    public static double convert(final String ingr, final String unit, final Double quantity) {
        switch (ingr) {
        case INGREDIENT_PREFIX + "egg":
        case INGREDIENT_PREFIX + "eggs":
            return 50 * quantity;
        default:
            return quantity * (unitsToGrams.get(unit) != null ? unitsToGrams.get(unit) : DEF_QNT);
        }
    }
}
